package com.macasa.panel.entity.impl;

import java.io.Serializable;
import java.util.ArrayList;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Version;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import org.hibernate.annotations.ForeignKey;

import com.google.common.base.Objects;
import com.macasa.panel.common.utils.CastHelpers;
import com.macasa.panel.entity.api.AbstractModel;
import com.macasa.panel.entity.api.IModel;
import com.macasa.panel.entity.api.IXenVds;
import com.macasa.panel.entity.api.IXenVdsInterface;
import com.macasa.panel.entity.impl.XenVdsInterfaces.IdXenVdsInterfaces;

@Entity
@Table(name = "xen_vds_interfaces")
@Getter
@Setter
@ToString(of = { "id" })
public class XenVdsInterfaces extends AbstractModel<IdXenVdsInterfaces> implements IModel<IdXenVdsInterfaces> {

	/**
	 * The Constant serialVersionUID
	 */
	@Version
	private static final long	serialVersionUID	= 8431963705821782431L;

	@Embeddable
	@Getter
	@Setter
	@AllArgsConstructor
	@ToString
	public class IdXenVdsInterfaces implements Serializable {

		/**
		 * The Constant serialVersionUID
		 */
		@Version
		private static final long	serialVersionUID	= 5688728795591013449L;

		@Column(name = "fk_server_id", nullable = false)
		private Integer				xenVdsId;

		@Column(name = "fk_interface_id", nullable = false)
		private Integer				xenVdsInterfaceId;

		@Override
		public boolean equals(final Object obj) {

			if (this == obj) {
				return true;
			}
			if (obj == null) {
				return false;
			}
			if (!(obj instanceof IdXenVdsInterfaces)) {
				return false;
			}

			final IdXenVdsInterfaces other = CastHelpers.cast(obj, IdXenVdsInterfaces.class);

			return Objects.equal(this.xenVdsId, other.xenVdsId) && Objects.equal(this.xenVdsInterfaceId, other.xenVdsInterfaceId);
		}

		@Override
		public int hashCode() {
			return Objects.hashCode(this.xenVdsId, this.xenVdsInterfaceId);
		}
	}

	@EmbeddedId
	private IdXenVdsInterfaces	id;

	@ManyToOne(fetch = FetchType.LAZY, targetEntity = com.macasa.panel.entity.impl.XenVds.class)
	@JoinColumn(name = "fk_server_id", insertable = false, updatable = false)
	@ForeignKey(name = "fk_xen_vds_interfaces_01")
	private IXenVds				xenVds;

	@ManyToOne(fetch = FetchType.LAZY, targetEntity = com.macasa.panel.entity.impl.XenVdsInterface.class)
	@JoinColumn(name = "fk_interface_id", insertable = false, updatable = false)
	@ForeignKey(name = "fk_xen_vds_interfaces_02")
	private IXenVdsInterface	xenVdsInterface;

	public XenVdsInterfaces(final IXenVds xenVds, final IXenVdsInterface xenVdsInterface) {

		// Alimentation des clés étrengaires
		this.setId(new IdXenVdsInterfaces(xenVds.getId(), xenVdsInterface.getId()));

		// Stockage des données
		this.xenVds = xenVds;
		this.xenVdsInterface = xenVdsInterface;

		// Alimentation unidirectionnelle
		if (this.xenVds.getInterfaces() == null) {
			this.xenVds.setInterfaces(new ArrayList<IXenVdsInterface>());
		}
		this.xenVds.getInterfaces().add(this.xenVdsInterface);
	}
}
